<?php

class CondominioModel extends AppModel {

	protected $_tableName = "condominio";

	public static function createHeadersToTable() {
		$headers = array( 
					array( "th" => "Nome" ),
					array( "th" => "Status" ),
					array( "th" => "Telefone" ),
					array( "th" => "Síndico" ),
					array( "th" => "Ações")
				);
		return $headers;
	}
	
	public static function listAllToTable(){
		$condominioModel = new CondominioModel();
		$results = $condominioModel->findAll();
		$msgNaoInformado = Messages::get("naoInformado");
		$response = array();
		foreach ( $results as $result ){
			$tmp = array();
			$tmp[ "id" ] = Encryption::encode( $result["id"] );
			$tmp[ "nome" ] = $result[ "nome" ];
			$tmp[ "status" ] = Status::convertToString($result[ "ativo" ]);
			$tmp[ "telefone" ] = Mask::maskNumber( $result[ "telefone" ], Mask::$TELEFONE);
			if( $result[ "sindico_id" ] != 0 ){
				$clienteModel = new ClienteModel();
				$sindico = $clienteModel->findById( $result["sindico_id"] );
				$tmp[ "sindico" ] =  $sindico["nome"];
			}else{
				$tmp[ "sindico" ] =  $msgNaoInformado;
			}
			$response[] = $tmp;
		}
		return $response;
	}
	
	public static function convertAllToSelectArray($valueSelected="") {
		$condominioModel = new CondominioModel();
		$condominios = $condominioModel->findAll(true);
		$response = array();
		$response[] = array( "option.value" => "0", "option.label" => "Selecione um condomínio" );
		foreach ($condominios as $condominio){
			$tmp = array();
			$tmp[ "option.value" ] = Encryption::encode($condominio['id']);
			if( $condominio['id'] == $valueSelected )
				$tmp[ "option.selected" ] = "selected";
			$tmp[ "option.label" ] = $condominio['nome'];
			$response[] = $tmp;
		}
		return $response;
	}
}

?>